//稀疏数组  和  
#include <iostream>
//有bug，如何用0初始化一个数组
#include <cstring>
void printArr(int **arr, int row, int col)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            std::cout << arr[i][j] << "\t";
        }
        std::cout << std::endl;
    }
}

int main(int argc, char **argv)
{
    //数组需要初始化，哪怕只初始化一个元素，不然系统会随机分配数值
    int arr[11][11] = {{0}};
    arr[1][1] = 11;
    arr[2][2] = 32;
    arr[4][5] = 48;
    //获取有效数值的数量
    //稀疏矩阵的行数=有效数值的数量+1；列数为3；
    int sum = 0;
    for (int i = 0; i < 11; i++)
    {
        for (int j = 0; j < 11; j++)
        {
            std::cout << arr[i][j] << "\t";
            if (arr[i][j] != 0)
                sum++;
        }
        std::cout << std::endl;
    }
    int sparseArr[sum + 1][3];
    sparseArr[0][0] = 11;
    sparseArr[0][1] = 11;
    sparseArr[0][2] = sum;
    int index = 1;
    for (int i = 0; i < 11; i++)
    {
        for (int j = 0; j < 11; j++)
        {
            if (arr[i][j] != 0)
            {
                sparseArr[index][0] = i;
                sparseArr[index][1] = j;
                sparseArr[index][2] = arr[i][j];
                index++;
            }
        }
    }

    for (int i = 0; i <= sum; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            std::cout << sparseArr[i][j] << "\t";
        }
        std::cout << std::endl;
    }

    int backArr[sparseArr[0][0]][sparseArr[0][1]] = {{0}};
    //这个居然可以初始化为0  
    memset(backArr,0,sizeof(backArr));
    for (int i = 0; i < sparseArr[0][0]; i++)
    {
        for (int j = 0; j < sparseArr[0][1]; j++)
        {
            std::cout << backArr[i][j] << "\t";
        }
        std::cout << std::endl;
    }
    std::cout<<std::endl;
    for (int i = 1; i <= sum; i++)
    {
        backArr[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
    }
    for (int i = 0; i < sparseArr[0][0]; i++)
    {
        for (int j = 0; j < sparseArr[0][1]; j++)
        {
            std::cout << backArr[i][j] << "\t";
        }
        std::cout << std::endl;
    }
}